package com.ddh.codeduck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codeduck.pojo.entity.SysDictType;
import com.ddh.codeduck.mapper.SysDictTypeMapper;
import com.codeduck.pojo.req.SysDictTypePageReq;
import com.ddh.codeduck.service.ISysDictTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author ddh
 * @since 2025-07-11
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    /**
     * 分页查询字典类型
     *
     * @param sysDictTypePageReq
     * @return
     */
    @Override
    public IPage<SysDictType> sysDictTypePage(SysDictTypePageReq sysDictTypePageReq) {
        Integer current = sysDictTypePageReq.getCurrent();
        Integer pageSize = sysDictTypePageReq.getPageSize();
        String dictName = sysDictTypePageReq.getDictName();
        String dictType = sysDictTypePageReq.getDictType();

        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dictName), SysDictType::getDictName, dictName);
        queryWrapper.like(StringUtils.isNotBlank(dictType), SysDictType::getDictType, dictType);
        // 默认按照创建时间排序，最新创建的在上面显示
        queryWrapper.orderByDesc(SysDictType::getCreatedAt);

        if (current == null || pageSize == null) {
            // 不分页，查询所有数据并封装到 IPage 中
            List<SysDictType> records = list(queryWrapper);
            Page<SysDictType> page = new Page<>();
            page.setRecords(records);
            page.setTotal(records.size());
            page.setSize(records.size());
            return page;
        } else {
            Page<SysDictType> page = new Page<>(current, pageSize);
            return page(page, queryWrapper);
        }
    }

}
